#include "compile_run.hpp"
#include"../comm/httplib.h"
using namespace httplib;
using namespace ns_compileandrun;

void Usage(std::string proc)
{
    std::cerr << "Usage: " << "\n\t" << proc << " port" << std::endl;
}

int main(int argc , char *argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        return 1;
    }
    Server svr;
    svr.Post("/compile_and_run", [](const Request &req, Response &res) {
        std::string in_json = req.body;
        std::string out_json;
        CompileAndRun::Start(in_json, &out_json);
        res.set_content(out_json, "application/json; charset=utf-8");
    });
    svr.listen("0.0.0.0",atoi(argv[1]));
    
    // std::string in_json;
    // Json::Value in_value;
    // in_value["code"] = R"(#include<iostream>
    // int main(){
    //     std::cout << "你可以看见我了" << std::endl;
    //     return 0;
    // })";
    // in_value["input"] = "";
    // in_value["cpu_limit"] = 1;
    // in_value["mem_limit"] = 10240*3;
    // Json::StyledWriter writer;
    // in_json = writer.write(in_value);
    // std::cout << in_json << std::endl;
    // std::string out_json;
    // CompileAndRun::Start(in_json, &out_json);
    //
    // std::cout << out_json << std::endl;
    return 0;
}
